package com.yu.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yu.crm.domain.pojo.CrmBlacklistType;
import com.yu.crm.domain.pojo.CrmBlacklistUser;
import com.yu.crm.domain.pojo.CrmRole;
import com.yu.crm.mapper.CrmBlacklistTypeMapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yu.crm.service.CrmBlacklistTypeService;
import com.yu.crm.service.CrmBlacklistUserService;
import com.yxzz.common.helper.DataHelpers;
import com.yxzz.common.helper.DateHelpers;
import com.yxzz.common.helper.ExceptionHelpers;
import com.yxzz.common.helper.valid.ValidatorHelpers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 黑名单分类表 服务实现类
 * </p>
 *
 * @author ct
 * @since 2020-07-01
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CrmBlacklistTypeServiceImpl extends ServiceImpl<CrmBlacklistTypeMapper, CrmBlacklistType> implements CrmBlacklistTypeService {
    @Autowired
    CrmBlacklistUserService crmBlacklistUserService;

    @Override
    public void removeBackListById(Long id) {
        //删除黑名单分类表中的数据
        this.removeById(id);

        //删除黑名单用户表中type为id 的所有数据
        QueryWrapper<CrmBlacklistUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", id);
        crmBlacklistUserService.remove(queryWrapper);
    }

    @Override
    public boolean saveOrUpdate(CrmBlacklistType entity) {


        if (DataHelpers.isEmpty(entity.getId())) {
            QueryWrapper<CrmBlacklistType> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("blacklist_type_name", entity.getBlacklistTypeName());
            int oldCount = super.count(queryWrapper);
            ValidatorHelpers.validateDataExist("分类名不能重复", oldCount);
            entity.setGmtCreate(DateHelpers.now());
            entity.setGmtModified(DateHelpers.now());
            return ExceptionHelpers.checkData(super.save(entity), "新增失败");
        } else {
            entity.setGmtModified(DateHelpers.now());
            return ExceptionHelpers.checkData(super.updateById(entity), "更新失败");
        }

    }
}

